package com.financial.sys.service.borrow.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.financial.model.pojo.TBorrow;
import com.financial.model.pojo.TUserWallet;
import com.financial.sys.mapper.borrow.TBorrowMapper;
import com.financial.sys.mapper.borrow.TUserWalletMapper;
import com.financial.sys.service.borrow.TBorrowService;
import com.financial.sys.util.FinanceCalcUtils;
import com.financial.sys.vo.bid.QueryObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hzy
 * @since 2020-02-12
 */
@Service("borrowService")
public class TBorrowServiceImpl extends ServiceImpl<TBorrowMapper, TBorrow> implements TBorrowService {

	@Autowired
	TUserWalletMapper tUserWalletMapper;
	@Autowired
	TBorrowMapper tBorrowMapper;

	@Override
	public Map<String, Object> getBorrowQuery(QueryObject queryObject) {
		Page<TBorrow> page = new Page<>(queryObject.getCurrentPage(), queryObject.getPageSize());
		Page<TBorrow> bidPage = page.setRecords(tBorrowMapper.getBorrowQuery(queryObject, page));
		HashMap<String, Object> map = new HashMap<>();
		map.put("currentPage", bidPage.getCurrent());
		map.put("pageSize", bidPage.getSize());
		//是否存在上一页
		if (bidPage.hasPrevious()) {
			map.put("prevPage", bidPage.getCurrent() - 1);
		} else {
			map.put("prevPage", bidPage.getCurrent());
		}
		//是否存在下一页
		if (bidPage.hasNext()) {
			map.put("nextPage", bidPage.getCurrent() + 1);
		} else {
			map.put("nextPage", bidPage.getCurrent());
		}
		map.put("totalCount", bidPage.getTotal());
		map.put("totalPage", bidPage.getPages());
		map.put("listData", bidPage.getRecords());
		return map;
	}

	@Override
	public Integer addBorrow(TBorrow borrow) {
		//借款人id
		String userId = borrow.getBorrowUserId();
		//借款金额
		Long borrowAmount = borrow.getBorrowAmount();
		//总利息
		long totalInterest = FinanceCalcUtils.calcTotalInterest(borrow.getRepaymentType(), borrow.getBorrowAmount(), borrow.getYearRate(), borrow.getRepaymentMonth());
		borrow.setTotalInterest(totalInterest);
		//申请时间
		borrow.setApplyTime(new Date());
		//发布审核中
		borrow.setBorrowState(10);
		//申请借款
		baseMapper.insert(borrow);
		//申请借款后,根据借款人id 授信额度相应减少
		TUserWallet tUserWallet = tUserWalletMapper.selectById(userId);
		//剩余授信额度
		Long residualCreditLine = tUserWallet.getCreditLine() - borrowAmount;
		//如果差<0返回0
		if (residualCreditLine < 0) {
			return 0;
		}
		tUserWallet.setResidualCreditLine(residualCreditLine);
		return tUserWalletMapper.updateById(tUserWallet);
	}

	@Override
	public Integer borrowAudit(String borrowId, Integer borrowState) {
		TBorrow tBorrow = tBorrowMapper.selectById(borrowId);
		tBorrow.setBorrowState(borrowState);
		//审核通过,设置招标开始时间和招标截止时间
		if (borrowState == 12) {
			tBorrow.setBorrowState(20);
			//发标时间
			tBorrow.setPublishTime(new Date());
			Calendar cal = Calendar.getInstance();
			cal.setTime(tBorrow.getPublishTime());
			cal.add(Calendar.DATE, tBorrow.getBidDays());
			//截至时间
			tBorrow.setBidDeadline(cal.getTime());
		}
		baseMapper.updateById(tBorrow);

		//获取借款人id
		TBorrow borrowById = baseMapper.selectById(borrowId);
		String userId = borrowById.getBorrowUserId();
		//获取借款人的额度
		Long borrowAmount = borrowById.getBorrowAmount();
		//审核拒绝,退还授信额度
		if (borrowState == 11) {
			TUserWallet tUserWallet = new TUserWallet();
			tUserWallet.setAccountId(userId);
			tUserWallet.setResidualCreditLine(tUserWallet.getResidualCreditLine() + borrowAmount);
			tUserWalletMapper.updateById(tUserWallet);
		}


		return 1;
	}

}
